var soapSelect = [{
		id:"0",
		contentType:"SOAP1.1"
	},
	{
		id:"1",
		contentType:"SOAP1.2"
	}
]

//初始化数据	
$(document).ready(function(){
	
	
	var optionFirst = new Option(soapSelect[0].contentType,soapSelect[0].id);
	var optionSecond = new Option(soapSelect[1].contentType,soapSelect[1].id);
	document.getElementById("soapSelect").options.add(optionFirst);
	document.getElementById("soapSelect").options.add(optionSecond);
	
	
	$('#comboxSearchfieldName').combobox({
		url: BASE_URL_FIELD_SEARCH + '/findAllOfCurrentUserCanManage' + '?v=' + Math.random(),
		valueField: 'id',
	    textField: 'name',
	    editable: false,
	    method:'get',
	    loadFilter: function(data){
	    	var allCheck = {'id':'','name':'全部'};
	    	data.content.unshift(allCheck);
	    	return data.content;
	    },
	    onSelect: function(record){
	    	comboxSearchSystemName.combobox({
                disabled: false,
                method:'get',
                queryParams: {fieldId: record.id},
                url: BASE_URL_FIELD_SYSTEM_SEARCH +'/findByFieldOfCurrentUserCanManage' + '?v=' + Math.random(),
                valueField: 'id',
                textField: 'name',
				data:'',
    		    loadFilter: function(data){
    		    	var allCheck = {'id':'','name':'全部'};
    		    	data.content.unshift(allCheck);
    		    	return data.content;
    		    },
    		    onLoadSuccess:function(){
    		    	comboxSearchSystemName.combobox('setValue','');
    		    }
            }).load();
	    },
	    onLoadSuccess:function(){
	    	$('#comboxSearchfieldName').combobox('setValue','');
	    }
	});
	
	//所属系统下拉框
	var comboxSearchSystemName = $('#comboxSearchSystemName').combobox({
        disabled: true,
        valueField: 'id',
        textField: 'name',
        editable: false,
	  data:[{id:'',name:'全部','selected':'true'}]
    });
	
	
	$('#comboxfieldName').combobox({
		url: BASE_URL_FIELD_SEARCH + '/findAllOfCurrentUserCanManage' + '?v=' + Math.random(),
		valueField: 'id',
	    textField: 'name',
	    editable: false,
	    method:'get',
	    loadFilter: function(data){
	    	var allCheck = {'id':'','name':'请选择'};
	    	data.content.unshift(allCheck);
	    	return data.content;
	    },
	    onSelect: function(record){
	    	comboxSystemName.combobox({
                disabled: false,
                method:'get',
                queryParams: {fieldId: record.id},
                url: BASE_URL_FIELD_SYSTEM_SEARCH +'/findByFieldOfCurrentUserCanManage' + '?v=' + Math.random(),
                valueField: 'id',
                textField: 'name',
				data:'',
    		    loadFilter: function(data){
    		    	var allCheck = {'id':'','name':'请选择'};
    		    	data.content.unshift(allCheck);
    		    	return data.content;
    		    },
    		    onLoadSuccess:function(){
    		    	/*comboxSystemName.combobox('setValue','');*/
    		    }
            }).load();
	    },
	    onLoadSuccess:function(){
	    	$('#comboxfieldName').combobox('setValue','');
	    }
	});
	
	//所属系统下拉框
	var comboxSystemName = $('#comboxSystemName').combobox({
        disabled: true,
        valueField: 'id',
        textField: 'name',
        editable: false,
	  data:[{id:'',name:'请选择','selected':'true'}]
    });
	
	
	$("#datagrid1").datagrid({
		url : BASE_URL_WSDL_META_DATA_SEARCH + '/findByExample' + '?v=' + Math.random(),
		method:'get',
		autoRowHeight:true,//自动行高
		singleSelect: true,//单选
		striped:true,
		nowrap:false,
		pagination:true,
		pageSize:20,
		pageList:[10,20,50,100],
		columns:[[
					{field:'',width:10,checkbox:true},
					{field:'fieldName',title:'所属领域',width:80},
					{field:'fieldSystemName',title:'所属系统',width:80},
					{field:'wsdlAddress',title:'wsdl地址',width:200},
					{field:'wsdlFilePath',title:'wsdl文件路径',width:200},
					{field:'operation',title:'操作',width:50, 
						formatter: function(value,row,index){
							return '<a href="#" class="default_btn"  onclick="test(' + index + ')">测试</a>';
						}
					}
		      ]],
		loadFilter:function(data){
			return {
				total: data.totalElements,
				rows: data.content
			};
		},
		onBeforeLoad:function(param){
			param.page -= 1;
			param.size = param.rows;
			delete param.rows;
			
			param.sort = 'createTime,desc';
			
			var fieldId = $('#comboxSearchfieldName').combobox('getValue');
			if(fieldId !=""){
				param.fieldId = fieldId;
			}
			
			var fieldSystemId = $('#comboxSearchSystemName').combobox('getValue');
			if(fieldSystemId !=""){
				param.fieldSystemId = fieldSystemId;
			}
			
			var wsdlAddress = $('#wsdlAddress').val();
			if(wsdlAddress !=""){
				param.wsdlAddress = wsdlAddress;
			}
			
			var wsdlFilePath = $('#wsdlFilePath').val();
			if(wsdlFilePath !=""){
				param.wsdlFilePath = wsdlFilePath;
			}
			
		}
	});
	
	$("#datagrid").datagrid('getPager').pagination({   
	    displayMsg:'当前显示从 [{from}] 到 [{to}] 共[{total}]条记录'
	}); 
	

});

function send() {
	var urlToOpen=$("#urlToOpen").val();
	var checkValue=$("#operationSelect").val();
	/*if(urlToOpen.indexOf("?wsdl")>-1){
		urlToOpen=urlToOpen.substring(0,urlToOpen.length-5);
	}*/
	
	var soapSelect = $("#soapSelect").val();
	
	var body=$("#request").val();
	
	var jsonData = {wsdlURL:urlToOpen, operation:checkValue, requestBody:body, soapProtocol:soapSelect};
	$.ajax({
		type: "get",
		url: BASE_URL_WSDL_META_DATA_CRUD + '/getSoapReponsetMsg' + '?v=' + Math.random(),
		dataType:'json',
		data: jsonData,
		contentType:"application/json",
		success: function(result) {
			//alert("result" + result.soapMessage);
			receive(result.soapReponseMsg);
		},
		error: function() {
			$.messager.alert('错误','<p>服务异常，测试获取webservice数据失败，请稍后重试或联系管理员!</p>', 'error');
		}
	});
	
}

function receive(data){
	var data = data.replace(/&lt;/g, "<").replace(/&gt;/g, ">");
	$("#response").val(data);
}

function getOperation() {
	var urlToOpen=$("#urlToOpen").val();
	if(urlToOpen==null||urlToOpen==""){
		$.messager.alert('警告','<p>请输入WSDL地址!</p>', 'warning');
		return;
	}
	var wsdlURL = {wsdlURL:urlToOpen};
	
	
	$.ajax({
		type: "get",
		url: BASE_URL_WSDL_META_DATA_CRUD + '/wsdlImportByUrl' + '?v=' + Math.random(),
		dataType:'json',
		data: wsdlURL,
		contentType:"application/json",
		success: function(result) {
			//alert("result" + result.wsdlOperations);
			if(result.backValue === true){
  				if(result.wsdlOperations == null || result.wsdlOperations == ""){
  					$.messager.alert('警告','<p>当前要测试的webservice中没有接口!</p>', 'warning');
  				}else{
  					setOperation(result.wsdlOperations);
  				}
			}else if(result.backValue === false){
				$.messager.alert('错误','<p>'+ result.errorDes +'!</p>', 'error');
			}else{
				$.messager.alert('错误','<p>服务异常，获取webservice方法失败，请稍后重试或联系管理员!</p>', 'error');	
			}
			
		},
		error: function() {
			$.messager.alert('错误','<p>服务异常，获取webservice方法失败，请稍后重试或联系管理员!</p>', 'error');
		}
	});
}

function setOperation(data) {
		if(data==null||data==""){
			
			return;
		}
		document.getElementById("operationSelect").innerHTML="";
		var list=new Array();
		list=data.split(","); //字符分割 
		var html="";
		for (i=0;i<list.length ;i++ )    
	    {    
	    	var option=new Option(list[i],list[i]);
	    	
	    	if(i==0){
	    		option.selected = true;
	    	}
	        document.getElementById("operationSelect").options.add(option);
	    } 
		getMessage();
	
	
}

function showMessage(data){
	var data = data.replace(/&lt;/g, "<").replace(/&gt;/g, ">");
	$("#request").val(data);
}

function getMessage(){
	var checkValue=$("#operationSelect").val();
	var urlToOpen=$("#urlToOpen").val();
	if(urlToOpen==null||urlToOpen==""){
		$.messager.alert('警告','<p>请输入WSDL地址!</p>', 'warning');
		return;
	}
	if(checkValue==null||checkValue==""){
		$.messager.alert('警告','<p>请选择操作方法!</p>', 'warning');
		return;
	}
	var jsonData = {wsdlURL:urlToOpen, operation:checkValue};
	$.ajax({
		type: "get",
		url: BASE_URL_WSDL_META_DATA_CRUD + '/getSoapRequestMsg' + '?v=' + Math.random(),
		dataType:'json',
		data: jsonData,
		contentType:"application/json",
		success: function(result) {
			//alert("result" + result.soapMessage);
			showMessage(result.soapRequestMsg);
		},
		error: function() {
			$.messager.alert('提示','<p>获取请求报文失败,请手动填写请求报文进行测试!</p>', 'info');
		}
	});
	
}

function saveData() {
	var urlToOpen=$("#urlToOpen").val();
	if(urlToOpen == "" || urlToOpen == null){
		$.messager.alert('警告','<p>wsdl地址不能为空!</p>', 'warning');
		return;
	}
	
	var fieldSystemId = $("#comboxSystemName").combobox('getValue');
	if(fieldSystemId == "" || fieldSystemId == null){
		$.messager.alert('警告','<p>请选择所属系统后再进行发布操作!</p>', 'warning');
		return;
	}
	
	
	var jsonData = {wsdlURL:urlToOpen, fieldSystemId:fieldSystemId};
	$.ajax({
		type: "get",
		url: BASE_URL_WSDL_META_DATA_CRUD + '/wsdlMetaDataSave' + '?v=' + Math.random(),
		dataType:'json',
		data: jsonData,
		contentType:"application/json",
		success: function(result) {
			//alert("result" + result.soapMessage);
			if(result.backValue == true){
				$.messager.alert('提示','<p>wsdl元数据发布成功!</p>', 'info', function(){
					$('#wsdlTabs').tabs('select', "wsdl元数据列表");
					$("#datagrid1").datagrid("reload", BASE_URL_WSDL_META_DATA_SEARCH + '/findByExample' + '?v=' + Math.random());
				});
			}else if(result.backValue == false){
				$.messager.alert('警告','<p>' + result.errorDes + '</p>', 'warning');
			}
		},
		error: function() {
			$.messager.alert('错误','<p>服务异常，发布wsdl元数据失败，请稍后重试或联系管理员!</p>', 'error');
		}
	});
	
}


function test(index){
	$('#wsdlTabs').tabs('select', "wsdl元数据测试");
	var row = $('#datagrid1').datagrid('getRows')[parseInt(index, 10)];
	
	$("#comboxfieldName").combobox('select', row.fieldId);
	$("#comboxSystemName").combobox('setValue', row.fieldSystemId);
	$("#urlToOpen").val(row.wsdlAddress);
}


function search(){
	//dataRetrieval('#txtBeginTime','#txtEndTime');
	$("#datagrid1").datagrid("reload", BASE_URL_WSDL_META_DATA_SEARCH + '/findByExample' + '?v=' + Math.random());
}
 
 	